﻿using System.Web.Mvc;
using Tau.Model;
using Tau.Service;
using Tau.ViewModels;

namespace Tau.Web.Areas.Admin.Controllers
{
    public class CategoryAdminController : BaseAdminController
    {
        public ActionResult Index()
        {
            //Get all images to display
            var svc = new TauService();

            var categorys = svc.GetAllCategories();

            return View("Index", new CategoryList() { Categories = categorys });
        }

        public ActionResult AddEditCategory(int? categoryId)
        {
            var svc = new TauService();

            Category editingCategory = categoryId.HasValue && categoryId > 0 ? svc.GetCategory(categoryId.Value) : new Category();

            return PartialView("AddEditCategory", new AddEditCategory
                                                  {
                                                      EditingCategory = editingCategory,
                                                      Mode = categoryId.HasValue ? DisplayMode.Edit : DisplayMode.Add
                                                  });
        }

        [HttpPost]
        public ActionResult AddEditCategory(AddEditCategory addEditCategory)
        {
            var svc = new TauService();

            if (addEditCategory.EditingCategory.CategoryId > 0)
            {
                svc.UpdateCategory(addEditCategory.EditingCategory);
            }
            else
            {
                Category newCategory = new Category()
                {
                    CategoryName = addEditCategory.EditingCategory.CategoryName
                };
                svc.AddCategory(newCategory);
            }
            
            return RedirectToAction("Index");
        }

        public ActionResult Delete(int categoryId)
        {
            var svc = new TauService();
            svc.DeleteCategory(categoryId);
            return RedirectToAction("Index");
        }
    }
}
